<template>
  <div class="about">
    <div class="user">
      <div class="info">
        <el-image
            style="border-radius: 50%; width: 150px; height: 150px"
            :src="imageUrl" fit="fit" >
        </el-image>
        <div class="userInfo">
          <span>{{name}}</span>
          <div class="btnBox">
            <el-button type="primary" size="mini" plain  @click="dialogVisible=true">编辑资料</el-button>
            <el-button type="primary" size="mini" plain @click="clear">清空考试记录</el-button>
          </div>
        </div>

      </div>
    </div>
    <el-dialog
        :visible.sync="dialogVisible"
        top="40vh"
        width="40vw"
    >
      <div >
        上传头像：
        <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :on-change="onChange"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
          <img v-if="imageUrl" :src="imageUrl" class="avatar" alt="">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </div>
      修改用户名：
      <el-input v-model="name"></el-input>

      <span slot="footer" class="dialog-footer"></span>
    </el-dialog>

    <div class="history">
      <div class="title">
        <span style="font-weight: bolder;font-size: 20px;color: #1e74c9">|</span>
        历史记录
      </div>
      <div class="list" style="text-align: center;margin:0 0 -20px;background-color: transparent">
        <div class="index" style="width: 5vw;text-align: center">序号</div>
        <div class="name" style="width: 35vw ;text-align: center">题目</div>
        <div class="score" style="width: 6vw;text-align: center">答对题数</div>
        <div class="number" style="width: 6vw;text-align: center">答错题数</div>
        <div class="percentage" style="width: 5vw;text-align: center">正确率</div>
      </div>
      <template v-if="list!=''">
      <div class="list" v-for=" (item,index) in list" :key="index" >
        <div class="index" style="width: 5vw;text-align: center">{{index+1}}</div>
        <div class="name" style="width: 35vw">{{ item.title }} · {{ item.type }} · {{ item.nums }}题</div>
        <div class="score" style="width: 5vw;text-align: center">{{ item.correct }}</div>
        <div class="number" style="width: 5vw;text-align: center">{{item.nums - item.correct}}</div>
        <div class="percentage" style="width: 5vw;text-align: center">{{item.percentage}}</div>
      </div>
      </template>
      <div class="null" v-else>

        <el-image
            style=" width: 150px; height: 150px"
            :src="require('../assets/null.png')" fit="fit" >
        </el-image>
        <div class="title" style=" text-align: center; font-size: 18px;">
          暂无数据，快去考试~
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default{
  name: 'About',
  data(){
    return{
      name:'用户逢考必过',
      list:'',
      imageUrl: '',
      dialogVisible:false
    }
  },
  created() {
    if (localStorage.getItem('history')!=null){
      console.log(JSON.parse(localStorage.getItem('history')))
      this.list = JSON.parse(localStorage.getItem('history'))
    }
    if (localStorage.getItem('history')!=null){
      console.log(JSON.parse(localStorage.getItem('history')))
      this.list = JSON.parse(localStorage.getItem('history'))
    }
  },
  watch:{
    name(){
      this.edit()
    }
  },
  methods:{
    onChange(){
      var _this = this;
      var event = event || window.event;
      var file = event.target.files[0];
      var reader = new FileReader();
      //转base64
      reader.onload = function(e) {
        _this.imageUrl = e.target.result //将图片路径赋值给src
      }
      reader.readAsDataURL(file);
      this.edit()
    },
    clear(){
      localStorage.removeItem('history')
      this.list = ''
    },
    edit(){
      let data = {
        img:this.imageUrl,
        name:this.name
      }
      localStorage.setItem('user',JSON.stringify(data))
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      console.log(this.imageUrl)
    },

    beforeAvatarUpload(file) {
      const isJPG = (file.type === 'image/jpeg' || file.type === 'image/png');
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG/png 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  }
}
</script>
<style>
.null{
  margin-top: 50px;
  /*background-color: #7ab2f6;*/
  height: 222px;
  width: 80vw;


}
  .about{
    width: 80vw;
    min-height: 80vh;
    border-radius: 15px;
    margin: 20px auto 0;
    background-color: white;
    border: 1px transparent solid;
  }
  .info{
    /*background-color: aquamarine;*/
    height: 150px;
    margin: 50px auto 0 150px;
    width:80%;
    display: flex;
  }
  .userInfo{
    font-size: 20px;
    font-weight: bolder;
    margin-left: 100px;
    display: flex;
    flex-direction: column;
    justify-content:space-around ;

  }
  span{
    text-align: left;
  }

  .btnBox{

  }
  .history{
    margin: 60px 50px 0;

  }
  .title{
    font-size: 20px;
    color: #363636;
    text-align: left;
    line-height: 20px;
  }
  .list{
    background-color: #f3f3f3;
    padding: 15px;
    border-radius: 15px;
    display: flex;
    margin: 20px 0;
    align-items: center;
    justify-content: space-between;
  }
  .index,.number,.name,.score,.percentage{
    font-weight: bolder;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    text-align: left;
    /*background-color: #2c3e50;*/
  }
  .index{
    text-align: center;
  }
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
